// 用于操作数据库
let query = require('../db/index')
let sql = require('./user_sql')

// 查询所有管理员数据 TODO
exports.getUser = function (callback) {
    query(sql.selectUser, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

//获取登录用户是否存在
exports.getLoginAccount = function (rec, callback) {
    var arr = [];
    arr.push(rec.username);
    arr.push(rec.password);
    arr.push('admin');
    query(sql.getLoginAccount, arr, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 返回指定用户记录 {}
exports.get = function (article_id, callback) {
    var arr = [];
    arr.push(article_id);
    query(sql.selectOne, arr, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 根据用户username查询用户信息
exports.getUsername = function (username, callback) {
    var arr = [];
    arr.push(username);
    query(sql.selectUsername, arr, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 增加一条记录
exports.add = function (rec, callback) {
    query(sql.insertOne, rec, function (err, results, fields) {
        if (err) {
            console.log('[增加记录错误] - ', err.message);
            throw err;
        }
        callback(results);
    });
}
// 查询角色数据 TODO
exports.getUserRole = function (callback) {
    query(sql.selectUserRole, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}
// 删除一条记录
exports.del = function (auother_id, callback) {
    var arr = [];
    arr.push(auother_id);
    query(sql.deleteOne, arr, function (err, results, fields) {
        if (err) {
            console.log('[删除记录错误] - ', err.message);
            throw err;
        }
        callback(results);
    });
}